﻿using B.S.Data.MES.API.Applications.QualityManagement.ExamineDesignCommand.Commands;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.QualityManagement.ExamineDesignCommand.CommandHandlers
{
    public class GetExamineDesignCommandHandler : IRequestHandler<GetExamineDesignCommand, APIRequst<APIPageing<List<ExamineDesign>>>>
    {
        private readonly IBaseRepository<ExamineDesign> _examinedesignRepository;
        public GetExamineDesignCommandHandler(IBaseRepository<ExamineDesign> examinedesignRepository)
        {
            _examinedesignRepository = examinedesignRepository;
        }
        public Task<APIRequst<APIPageing<List<ExamineDesign>>>> Handle(GetExamineDesignCommand request, CancellationToken cancellationToken)
        {
            var list = _examinedesignRepository.GetAll().Where(x => !x.isDel);
            if(!string.IsNullOrEmpty(request.DesignName))
            {
                list = list.Where(x => x.DesignName.Contains(request.DesignName));
            }
            if(request.DesugbType>0)
            {
                list=list.Where(x=>x.DesugbType==request.DesugbType);
            }
            var recordcount = list.Count();
            var pagecount = (int)Math.Ceiling(recordcount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.DesignId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            APIPageing<List<ExamineDesign>> paing = new APIPageing<List<ExamineDesign>>();
            paing.Total = pagecount;
            paing.TotalCount = recordcount;
            paing.Data = list.ToList();

            return Task.FromResult(new APIRequst<APIPageing<List<ExamineDesign>>>
            {

                Data = paing,
                Code = RequstCode.成功,
                Msg=RequstCode.成功.ToString()

            });


        }
    }
}
